<?php
/**
 * Created by PhpStorm.
 * Date: 2016/7/28
 * Time: 17:37
 */

/**
 * 发送验证码 同时验证手机号码是否已经使用过
 * 如果是未使用的手机号
 * 发送验证码，并将验证码保存到phone_check
 * phone_check：如果手机号已经在表中存在，则是更新，如果不存在，则是插入
 */

$RootDir = $_SERVER['DOCUMENT_ROOT'];
include $RootDir.'/utils/mysql/MysqlUtils.php';

$phone=$_POST['phone']; //手机号码
$check_type=$_POST['check_type']; //默认是要验证手机号码是否已经被注册的，如果为1，则可以不用到user_info中检测

$curtime = time();
$endtime = $curtime + 120;

//$phone="";
$cout = -1;
$conn = conn_mysql();

function check_phone($phone,$conn){
    //判断手机号码是否使用
    $query = "select count(1) as cout from user_info where phone_num = '$phone' limit 1";
    #echo $query;
    $result = queryRecord($query,$conn);
    while ($row=mysql_fetch_array($result, MYSQL_ASSOC)){
        $cout = $row['cout'];
    }
    if ($cout == 1)die("err: 手机号码已经被占用");
}

function create_check($phone,$conn,$curtime,$endtime){
    $check_info = "6666";
//判断是否以前发送过验证码
    $query = "select count(1) as cout from phone_check where phone = '$phone' limit 1";
    $result = queryRecord($query,$conn);
    while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
        if ($row['cout'] == '1') {
            //如果发送过，则更新验证码
            $query = "update phone_check set check_info = '$check_info',starttime = '$curtime', endtime = '$endtime' where phone = '$phone'";
        } else {
            //如果未发送过，则插入一条记录
            $query = "insert into phone_check(phone,check_info,starttime,endtime) VALUES ('$phone','$check_info','$curtime','$endtime')";
        }
    }

    if (!mysql_query($query,$conn)) {
        die('err: ' . mysql_error());
    }
}

if($check_type != 1)check_phone($phone,$conn);
create_check($phone,$conn,$curtime,$endtime);

//发送验证码短信
echo "OK";

mysql_close($conn);


